package com.tenglong.teacher.ui.nimchat;



import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.constant.MsgDirectionEnum;
import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.nimlib.sdk.nos.model.NosThumbParam;
import com.netease.nimlib.sdk.nos.util.NosThumbImageUtil;
import com.netease.nimlib.sdk.uinfo.model.UserInfo;
import com.tenglong.teacher.data.requestInfo.AnswerQuestionItem;


import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.netease.nim.uikit.common.ui.imageview.HeadImageView.DEFAULT_AVATAR_THUMB_SIZE;

public class NimUtils {
    public static int ANSWER_QESTION_TYPE=0;
    public static int HOMEWORK_TYPE=1;
    public static int WRONG_TYPE=-1;
    public static IMMessage createSessionIdUtiMesssage(int type, String sessionId) {
        IMMessage msg = MessageBuilder.createTextMessage("null", SessionTypeEnum.P2P, "NULL");
        return addSessionId(msg,type,sessionId);

    }
    public static IMMessage createQuestionSessionIdUtiMesssage(AnswerQuestionItem item, int type, String sessionId) {
        IMMessage msg = MessageBuilder.createTextMessage("null", SessionTypeEnum.P2P, "NULL");
        msg.setDirect(MsgDirectionEnum.Out);

        msg.setStatus(MsgStatusEnum.read);
        return addSessionIdWithOher(msg,type,sessionId,item);



    }


    public static IMMessage addSessionId(IMMessage msg, int type, String sessionId){
        Map<String, Object> extSessionId = new HashMap<>(2);
        extSessionId.put("sessionId",sessionId);
        extSessionId.put("type",Integer.valueOf(type));
        msg.setRemoteExtension(extSessionId);
        return msg;

    }
    public static IMMessage addSessionIdWithOher(IMMessage msg, int type, String sessionId, AnswerQuestionItem object){
        Map<String, Object> extSessionId = new HashMap<>(5);
        extSessionId.put("sessionId",sessionId);
        extSessionId.put("type",Integer.valueOf(type));
        extSessionId.put("zuiwenuri",object.getQuestioning());
        extSessionId.put("zuiwenimguri",object.getFirstpicmsg());
        extSessionId.put("zuiwentxt",object.getFirsttextmsg());
        msg.setRemoteExtension(extSessionId);
        return msg;

    }
    public static int getMsgType(IMMessage msg){
        Map<String, Object> ext  =msg.getRemoteExtension();
        if(ext!=null){
            return((Integer)ext.get("type")).intValue();
        }else{
            return WRONG_TYPE;
        }

    }
    public static String getMsgSessionId(IMMessage msg){
        Map<String, Object> ext  =msg.getRemoteExtension();
        if(ext!=null){
            return ext.get("sessionId").toString();
        }else{
            return null;
        }

    }
    public static boolean shouldDropIncomingMsg(IMMessage msg, int type, String sessionId){
        Map<String, Object> ext  =msg.getRemoteExtension();
        if(ext==null){
            return true;
        }
        String sessionIdMsg = ext.get("sessionId").toString();
        int typeMsg = ((Integer)ext.get("type")).intValue();
        if(ext!=null){
            if(sessionId!=null && type !=WRONG_TYPE && sessionId.equals(sessionIdMsg) && typeMsg ==type){
                 return false;
            }else{
                return true;
            }

        }else{
            return true;
        }

    }
    public static List<IMMessage> dropIncomingMsg(List<IMMessage> msgs, int type, String sessionId){
        Iterator<IMMessage> iterator = msgs.iterator();
        while(iterator.hasNext()){
            IMMessage ims = iterator.next();
            if(shouldDropIncomingMsg(ims,type,sessionId)){
                iterator.remove();
            }

        }
        return msgs;

    }
    public static List<IMMessage> fetchMessagesWithSessionId(List<IMMessage> messages, String sessionId){
        Iterator<IMMessage> iterator = messages.iterator();
        while(iterator.hasNext()){
            IMMessage ims = iterator.next();
            Map<String, Object> ext  =ims.getRemoteExtension();
            if(ext==null){
                iterator.remove();
            }
            if (ext != null && !ext.isEmpty()) {
                if(ext.get("sessionId")==null||!ext.get("sessionId").equals(sessionId)){
                    iterator.remove();
                }
            }
            //do something
        }
        return messages;

    }
    public static void loadBuddyAvatar(Context context, ImageView imageView, String account, String uri) {
        final UserInfo userInfo = NimUIKit.getUserInfoProvider().getUserInfo(account);
        doLoadImage(context,imageView,userInfo != null ?  uri+userInfo.getAvatar() : null, com.netease.nim.uikit.R.drawable.nim_avatar_default, DEFAULT_AVATAR_THUMB_SIZE);
    }
    public  static String makeAvatarThumbNosUrl(final String url, final int thumbSize) {
        if (TextUtils.isEmpty(url)) {
            return url;
        }

        return thumbSize > 0 ? NosThumbImageUtil.makeImageThumbUrl(url, NosThumbParam.ThumbType.Crop, thumbSize, thumbSize) : url;
    }
    public static  void doLoadImage(Context context, ImageView imageView,final String url, final int defaultResId, final int thumbSize) {
        /*
         * 若使用网易云信云存储，这里可以设置下载图片的压缩尺寸，生成下载URL
         * 如果图片来源是非网易云信云存储，请不要使用NosThumbImageUtil
         */
        final String thumbUrl = makeAvatarThumbNosUrl(url, thumbSize);
        RequestOptions requestOptions = new RequestOptions()
                .centerCrop()
                .placeholder(defaultResId)
                .error(defaultResId)
                .override(thumbSize, thumbSize);
        Glide.with(context.getApplicationContext()).asBitmap()
                .load(thumbUrl)
                .apply(requestOptions)
                .into(imageView);
    }
}



